package com.atguigu.realtime.app.dwm;

import com.alibaba.fastjson.JSON;
import com.atguigu.realtime.app.BaseAppV2;
import com.atguigu.realtime.bean.OrderWide;
import com.atguigu.realtime.bean.PaymentInfo;
import com.atguigu.realtime.bean.PaymentWide;
import com.atguigu.realtime.util.MyKafkaUtil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.ProcessJoinFunction;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.Map;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/2/8 14:51
 */
public class DWMPaymentWideApp extends BaseAppV2 {
    public static void main(String[] args) {
        new DWMPaymentWideApp().init(1, "DWMPaymentWideApp", "dwd_payment_info", "dwm_order_wide");
    }
    
    @Override
    protected void run(StreamExecutionEnvironment env,
                       Map<String, DataStreamSource<String>> sourceStreams) {
        setWebUi(env, 9999);
        // 1. 支付事实表
        final KeyedStream<PaymentInfo, Long> paymentInfoKS = sourceStreams
            .get("dwd_payment_info")
            .map(json -> JSON.parseObject(json, PaymentInfo.class))
            .assignTimestampsAndWatermarks(
                WatermarkStrategy
                    .<PaymentInfo>forBoundedOutOfOrderness(Duration.ofSeconds(5))
                    .withTimestampAssigner((info, ts) -> toTs(info.getCreate_time()))
            )
            .keyBy(PaymentInfo::getOrder_id);
        paymentInfoKS.print("paymentInfoKS...");
        
        // 2. orderWide实时表
        final KeyedStream<OrderWide, Long> orderWideKS = sourceStreams
            .get("dwm_order_wide")
            .map(json -> JSON.parseObject(json, OrderWide.class))
            .assignTimestampsAndWatermarks(
                WatermarkStrategy
                    .<OrderWide>forBoundedOutOfOrderness(Duration.ofSeconds(5))
                    .withTimestampAssigner((orderWide, ts) -> toTs(orderWide.getCreate_time()))
            )
            .keyBy(OrderWide::getOrder_id);
        orderWideKS.print("orderWideKS...");
        
        // 3. 进行 interval join
        final SingleOutputStreamOperator<PaymentWide> paymentWideStream = paymentInfoKS
            .intervalJoin(orderWideKS)
            .between(Time.minutes(-45), Time.minutes(0)) // 明细数据一定早于支付数据.  假设支付时间最多延迟45分钟
            .process(new ProcessJoinFunction<PaymentInfo, OrderWide, PaymentWide>() {
                @Override
                public void processElement(PaymentInfo paymentInfo,
                                           OrderWide orderWide,
                                           Context ctx,
                                           Collector<PaymentWide> out) throws Exception {
                    System.out.println("processElement...");
                    out.collect(new PaymentWide(paymentInfo, orderWide));
                }
            });
        
        // 4.数据写入到 Kafka
        paymentWideStream
            .map(JSON::toJSONString)
            .addSink(MyKafkaUtil.getKafkaSink("dwm_payment_wide"));
    }
    
    public static Long toTs(String dateTime) {
        try {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateTime).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
    
}
